import { IFrameState, IEvent } from "./model";

function init (): IFrameState {
  return {
    leftWidth: 250,
    virtualLeftWidth: null,
  };
}

function onLeftResize (e: IEvent, s: IFrameState): IFrameState {
  const movementX: number = e.data;
  const virtualLeftWidth = s.virtualLeftWidth + movementX;
  let leftWidth = virtualLeftWidth;
  if (leftWidth < 200) {
    leftWidth = 200;
  } else if (leftWidth > 500) {
    leftWidth = 500;
  }
  return {
    ...s,
    leftWidth,
    virtualLeftWidth
  }
}

function onLeftResizeBegin (e: IEvent, s: IFrameState): IFrameState {
  return {
    ...s,
    virtualLeftWidth: s.leftWidth
  }
}

function onLeftResizeEnd (e: IEvent, s: IFrameState): IFrameState {
  return {
    ...s,
    virtualLeftWidth: null,
  }
}

export function dispatch (e: IEvent, s: IFrameState): IFrameState {
  if (e.type == 'app.init') {
    return init();
  } else if (e.type == 'frame.left.resize') {
    return onLeftResize(e, s);
  } else if (e.type == 'frame.left.beginResize') {
    return onLeftResizeBegin(e, s);
  } else if (e.type == 'frame.left.endResize') {
    return onLeftResizeEnd(e, s);
  } else {
    return s;
  }
}

